// subpkg/movie-detail/movie-detail.js
import { getMovieDetail } from '../../api/movie'
import { getUserInfo , updateUserInfo} from '../../api/user'
Page({
  data: {
    movieDetail: {},
    isOpen: false // 是否展开
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '拼命加载中...'
    })
    if (options.userid) {
      this.updateScore(options.userid)
    }
    this.getMovieDetailData(options.id)
  },
  async updateScore(userid) {
    await updateUserInfo({
      userid,
      score: 10
    })
  },
  async getMovieDetailData(movieId) {
    const res = await getMovieDetail(movieId)

    this.setData({
      movieDetail: res
    }, () => {
      wx.setNavigationBarTitle({
        title: res.movie.nm,
      })
      wx.hideLoading()
    })
  },
  openOrFold() {
    this.setData({
      isOpen: !this.data.isOpen
    })
  },
  async onShareAppMessage() {
    const token = wx.getStorageSync('token')
    if (token) {
      const res = await await getUserInfo()

      return {
        title: this.data.movieDetail.movie.nm,
        imageUrl: this.data.movieDetail.movie.img,
        path: `/subpkg/movie-detail/movie-detail?id=${this.data.movieDetail.movie.id}&userid=${res.data.userid}`
      }
    } else {
      wx.redirectTo({
        url: '/subpkg/login/login',
      })
    }
  },
  async onShareTimeline() {
    const token = wx.getStorageSync('token')
    if (token) {
      const res = await await getUserInfo()

      return {
        title: this.data.movieDetail.movie.nm,
        imageUrl: this.data.movieDetail.movie.img,
        path: `/subpkg/movie-detail/movie-detail?id=${this.data.movieDetail.movie.id}&userid=${res.data.userid}`
      }
    } else {
      wx.redirectTo({
        url: '/subpkg/login/login',
      })
    }
  },
  // 去购票
  purchaseTicket() {
    wx.navigateTo({
      url: `/subpkg/purchase/purchase?movieId=${this.data.movieDetail.movie.id}&cityId=30`,
    })
  }
})